package com.wdb007.venus.svc.biz.card;

import java.math.BigDecimal;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wdb007.venus.svc.dal.account.dao.AccountCashDAO;
import com.wdb007.venus.svc.dal.account.dao.AccountCashLogDAO;
import com.wdb007.venus.svc.dal.account.model.AccountCashDO;
import com.wdb007.venus.svc.dal.account.model.AccountCashLogDO;
import com.wdb007.venus.svc.dal.card.dao.RechargeableCodeLogDAO;
import com.wdb007.venus.svc.dal.card.dao.RechargeableCodeRuleDAO;
import com.wdb007.venus.svc.dal.card.model.RechargeableCodeLogDO;
import com.wdb007.venus.svc.dal.card.model.RechargeableCodeRuleDO;
import com.wdb007.venus.svc.facade.CardService;
import com.wdb007.venus.svc.facade.constant.BizConstants;
import com.wdb007.venus.svc.facade.constant.RspCodeEnum;
import com.wdb007.venus.svc.facade.exception.BizException;

@Service("cardService")
public class CardServiceImpl implements CardService {

	@Autowired
	private RechargeableCodeLogDAO rechargeableCodeLogDAO;
	@Autowired
	private RechargeableCodeRuleDAO rechargeableCodeRuleDAO;
	
	@Autowired
	private AccountCashDAO accountCashDAO;
	@Autowired
	private AccountCashLogDAO accountCashLogDAO;
	
	@Override
	public BigDecimal charge(Long userId, String code) throws BizException {
		//1.首先判断该用户是否使用过该充值码
		RechargeableCodeLogDO query = new RechargeableCodeLogDO();
		query.setCode(code);
		query.setUserId(userId);
		Integer c = rechargeableCodeLogDAO.countByCondition(query);
		if(c != null && c != 0) {
			throw new BizException(RspCodeEnum.CARD_CHARGE_IS_USED);
		}
		//2.判断该充值码是否已经过期
		RechargeableCodeRuleDO rule = rechargeableCodeRuleDAO.getByCode(code);
		if(null == rule) {
			throw new BizException(RspCodeEnum.CARD_CHARGE_CODE_IS_WRONG);
		}
		if(0 == rule.getValidFlag()) {
			throw new BizException(RspCodeEnum.CARD_CHARGE_IS_INVAILD);
		}
		//3.进行充值
		AccountCashDO accountCash = accountCashDAO.getByUserid(userId);
		accountCash.setCashAmount(accountCash.getCashAmount().add(rule.getAmount()));
		accountCash.setModifyTime(new Date());
		accountCashDAO.update(accountCash);
		AccountCashLogDO cashLog = new AccountCashLogDO();
		cashLog.setAccountCashId(accountCash.getId());
		cashLog.setChangeAmount(rule.getAmount());
		cashLog.setChargeType(BizConstants.ACCOUNT_CASH_CHARGE_TYPE_60);
		cashLog.setOperType(BizConstants.ACCOUNT_CASH_OPER_TYPE_10);
		cashLog.setCreateTime(new Date());
		accountCashLogDAO.insert(cashLog);
		
		query = new RechargeableCodeLogDO();
		query.setCode(code);
		c = rechargeableCodeLogDAO.countByCondition(query);
		if(c != null && (c+1) == rule.getChargeCount()) {
			rule.setValidFlag(0);
			rule.setModifyTime(new Date());
			rechargeableCodeRuleDAO.update(rule);
		}
		
		RechargeableCodeLogDO logDO = new RechargeableCodeLogDO();
		logDO.setAmount(rule.getAmount());
		logDO.setCode(code);
		logDO.setCreateTime(new Date());
		logDO.setUserId(userId);
		rechargeableCodeLogDAO.save(logDO);
		return rule.getAmount();
	}

}
